package com.ruoyi.bookkeeping.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bookkeeping.domain.BookSort;
import com.ruoyi.bookkeeping.domain.param.UpdateBookFriendsParam;
import com.ruoyi.bookkeeping.domain.vo.BookFriendsVo;
import com.ruoyi.bookkeeping.mapper.BookSortMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bookkeeping.mapper.BookFriendsMapper;
import com.ruoyi.bookkeeping.domain.BookFriends;
import com.ruoyi.bookkeeping.service.IBookFriendsService;

/**
 * 好友Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-24
 */
@Service
public class BookFriendsServiceImpl extends ServiceImpl<BookFriendsMapper, BookFriends> implements IBookFriendsService
{
    @Autowired
    private BookFriendsMapper bookFriendsMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询好友
     * 
     * @param id 好友主键
     * @return 好友
     */
    @Override
    public BookFriends selectBookFriendsById(Long id)
    {
        return bookFriendsMapper.selectBookFriendsById(id);
    }

    /**
     * 查询好友列表
     * @param userId
     * @return
     */
    @Override
    public List<BookFriendsVo> selectBookFriendsList(Long userId)
    {
        LambdaQueryWrapper<BookFriends> bookFriendsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookFriendsLambdaQueryWrapper.eq(BookFriends::getUserId,userId);
        List<BookFriends> bookFriends = baseMapper.selectList(bookFriendsLambdaQueryWrapper);
        List<BookFriendsVo> bookFriendsVos = new ArrayList<>();
        for(BookFriends bookFriends1:bookFriends){
            BookFriendsVo bookFriendsVo = new BookFriendsVo();
            BeanUtils.copyProperties(bookFriends1,bookFriendsVo);
            bookFriendsVos.add(bookFriendsVo);
        }
        return bookFriendsVos;
    }


    /**
     * 修改好友备注
     * @param bookFriends
     * @param userId
     * @return
     */
    @Override
    public int updateBookFriends(UpdateBookFriendsParam bookFriends,Long userId)
    {
        LambdaQueryWrapper<BookFriends> bookFriendsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookFriendsLambdaQueryWrapper.eq(BookFriends::getUserId,userId)
                .eq(BookFriends::getFriendsId,bookFriends.getFriendsId());
        BookFriends bookFriends1 = baseMapper.selectOne(bookFriendsLambdaQueryWrapper);
        bookFriends1.setFriendsRemark(bookFriends.getFriendsRemark());
        return baseMapper.updateById(bookFriends1);
    }


    /**
     * 删除好友信息
     * 
     * @param id 好友主键
     * @return 结果
     */
    @Override
    public int deleteBookFriendsById(Long id,Long userId)
    {
        LambdaQueryWrapper<BookFriends> bookFriendsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookFriendsLambdaQueryWrapper.eq(BookFriends::getUserId,userId)
                .eq(BookFriends::getFriendsId,id);
        baseMapper.delete(bookFriendsLambdaQueryWrapper);
        LambdaQueryWrapper<BookFriends> bookFriendsLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        bookFriendsLambdaQueryWrapper1.eq(BookFriends::getUserId,id)
                .eq(BookFriends::getFriendsId,userId);
        return baseMapper.delete(bookFriendsLambdaQueryWrapper1);
    }

    @Override
    public List<BookFriendsVo> findFriends(String username,String nickName,Long userId) {
        List<BookFriendsVo> bookFriendsVos = new ArrayList<>();
        SysUser searchSysUser =new SysUser();
        if(StrUtil.isNotBlank(username)){
            searchSysUser.setUserName(username);
        }
        if(StrUtil.isNotBlank(nickName)){
            searchSysUser.setNickName(nickName);
        }
        List<SysUser> sysUsers = sysUserMapper.selectUserList(searchSysUser);
        for(SysUser sysUser:sysUsers){
            BookFriendsVo bookFriendsVo = new BookFriendsVo();
            bookFriendsVo.setFriendsId(sysUser.getUserId());
            bookFriendsVo.setFriendsRemark(sysUser.getNickName());
            LambdaQueryWrapper<BookFriends> bookFriendsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            bookFriendsLambdaQueryWrapper.eq(BookFriends::getUserId,userId).eq(BookFriends::getFriendsId,sysUser.getUserId());
            BookFriends bookFriends = baseMapper.selectOne(bookFriendsLambdaQueryWrapper);
            if(ObjectUtil.isNull(bookFriends) && !ObjectUtil.equal(bookFriendsVo.getFriendsId(),userId)){
                bookFriendsVo.setIsGoodFriends(0);
            }else if(ObjectUtil.equal(bookFriendsVo.getFriendsId(),userId)){
                bookFriendsVo.setIsGoodFriends(2);
            }else {
                bookFriendsVo.setIsGoodFriends(1);
            }
            bookFriendsVos.add(bookFriendsVo);
        }
        bookFriendsVos.sort(Comparator.comparing(BookFriendsVo::getIsGoodFriends));
        return bookFriendsVos;
    }

    @Override
    public String getFriendName(Long userId, Long friendId) {
        LambdaQueryWrapper<BookFriends> bookFriendsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookFriendsLambdaQueryWrapper.eq(BookFriends::getFriendsId,friendId)
                .eq(BookFriends::getUserId,userId);
        BookFriends bookFriends = baseMapper.selectOne(bookFriendsLambdaQueryWrapper);
        return bookFriends.getFriendsRemark();
    }
}
